(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.acctCcyEditController', accountCcyEditController);
	accountCcyEditController.$inject = [ '$scope', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'accountService'];
	function accountCcyEditController($scope, $uibModalInstance, Constants, dialogsModule, gridService, layoutConfig, validatorConfig, authService, accountService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("accountCcyEditPopup"), datas = [], datasTwo = [];
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		var layout1=ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoPrefix.patternOptions;
		var layout2=ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoSuffix.patternOptions;
		ctrl.validatorConfig = validatorConfig;
		ctrl.formName = 'accountCcyPopupForm';
		ctrl.ok = function() {
			if($scope.accountCcyPopupForm) {
				$scope.accountCcyPopupForm.$setSubmitted();
				if(!$scope.accountCcyPopupForm.$valid) {
					dialogsModule.showErrorMessages($scope.accountCcyPopupForm);
					return;
				}
			}
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.acctCcy) !== false) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.setPatternNames = function() {
			if(ctrl.judge =="Y"){
				if (ctrl.acctCcy.acctPatternFlag == "N") {
					delete ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoPrefix.patternOptions;
					delete ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoSuffix.patternOptions;
				} else {
					ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoPrefix.patternOptions = layout1;
					ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoSuffix.patternOptions = layout2;
				}
				ctrl.judge="N";
			}else if(ctrl.judge =="N"){
				if (ctrl.acctCcy.acctPatternFlag == "N") {
					delete ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoPrefix.patternOptions;
					delete ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoSuffix.patternOptions;
					ctrl.acctCcy.acctNoPrefix = null;
					ctrl.acctCcy.acctNoSuffix = null;
					ctrl.acctChanged();
					ctrl.acctChangedTwo();
					
				} else {
					ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoPrefix.patternOptions = layout1;
					ctrl.layoutConfig.formConfigs.general.gridConfigs.acctNoSuffix.patternOptions = layout2;
					ctrl.acctCcy.acctNoPrefix = null;
					ctrl.acctCcy.acctNoSuffix = null;
					ctrl.acctChanged();
					ctrl.acctChangedTwo();
					
				}
			}
		};
		
		ctrl.acctChanged = function(){
			ctrl.accountPatterns = [];
			for(var j=0; j < datas.length; j++){
				ctrl.accountPatterns.push(datas[j]);
			}
			if(ctrl.accountPatterns.length > 0 && ctrl.acctCcy.acctNoPrefix != null){
				var m=[], count=[];
				 for(var i=0; i < ctrl.accountPatterns.length; i++){
					 count.push(i);
					 if(ctrl.acctCcy.acctNoPrefix.indexOf(ctrl.accountPatterns[i].value) != -1){
						m.push(i);
					 }
				 }
				if(m.length > 0){
					var purify = function(arr1,arr2){
					  var as = [];
					  var arr ="."+arr1.join(".")+".";
					    for(var k=0; k<arr2.length; k++){
					        arr.indexOf("."+arr2[k]+".") === -1 ? as.push(datas[k]) : 0; 
					    }  
					    return as;  
					};
					ctrl.accountPatterns = purify(m, count);
				} 
			}
		};
		
		ctrl.acctChangedTwo = function(){
			ctrl.accountPatternsTwo = [];
			for(var j=0; j < datasTwo.length; j++){
				ctrl.accountPatternsTwo.push(datasTwo[j]);
			}
			if(ctrl.accountPatternsTwo.length > 0 && ctrl.acctCcy.acctNoSuffix != null){
				var m=[], count=[];
				 for(var i=0; i < ctrl.accountPatternsTwo.length; i++){
					 count.push(i);
					 if(ctrl.acctCcy.acctNoSuffix.indexOf(ctrl.accountPatternsTwo[i].value) != -1){
						m.push(i);
					 }
				 }
				if(m.length > 0){
					var purify = function(arr1,arr2){
					  var as = [];
					  var arr ="."+ arr1.join(".")+".";
					    for(var k=0;k<arr2.length;k++){  
					        arr.indexOf("."+arr2[k]+".") === -1 ? as.push(datasTwo[k]) : 0;
					    }  
					    return as;  
					};
					ctrl.accountPatternsTwo = purify(m, count);
				} 
			}
		};
		
		ctrl.init = function() {
			accountService.getPatternName().then(function(patternNames){
				datas = patternNames;
				datasTwo = patternNames;
			});
			accountService.initOptions().then(function(result){
				//Initialization data
				ctrl.custAcctFlagOptions = result.custAcctFlagOptions;
				ctrl.acctPatternFlagOptions = result.acctPatternFlagOptions;
				ctrl.splitFlagOptions = result.splitFlagOptions;
				ctrl.summaryFlag1Options = result.summaryFlag1Options;
				ctrl.summaryFlag2Options = result.summaryFlag2Options;
				ctrl.acctTypeOptions = result.acctTypeOptions;
				ctrl.partyTypeOptions = result.partyTypeOptions;
				ctrl.acctCcyOptions = result.acctCcyOptions;
				ctrl.acctNoOptions = result.acctNoOptions;
				ctrl.accountPatterns = result.accountPatterns;
				ctrl.accountPatternsTwo = result.accountPatternsTwo;
				
				if (ctrl.dialogsModule.params.acctCcy) {
					ctrl.acctCcy = ctrl.dialogsModule.params.acctCcy;
				} else {
					ctrl.acctCcy = {};
				}
				if (ctrl.dialogsModule.params.acctCcyReadonly) {
					ctrl.acctCcyReadonly = ctrl.dialogsModule.params.acctCcyReadonly;
				} else {
					ctrl.acctCcyReadonly = 'N';
				}
				//acctCcyFilterOptions
				if (ctrl.dialogsModule.params.acctCcyList) {
					ctrl.acctCcyList = ctrl.dialogsModule.params.acctCcyList;
				} else {
					ctrl.acctCcyList = [];
				}
				ctrl.acctCcyFilterOptions = angular.copy(ctrl.acctCcyOptions);
				ctrl.judge="Y";
				angular.forEach(ctrl.acctCcyList, function(data, index1, array){
					angular.forEach(ctrl.acctCcyOptions.data, function(option, index2, options){
						if(data.acctCcy == option.value){
							ctrl.acctCcyFilterOptions.data.splice(index2, 1);
							ctrl.acctCcyOptions = angular.copy(ctrl.acctCcyFilterOptions);
						}
					});
				});
			});
		};
		
	}
})(angular);